package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.Chess;
import com.sghd.fight.module.game.core.ChessBoard;
import com.sghd.fight.module.game.core.Grid;
import com.sghd.fight.module.game.core.model.ChessState;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.ChessType;
import com.sghd.pojo.fight.game.SkillReport;
import com.sghd.pojo.fight.game.TargetReport;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author frank
 * @date 2018-12-12 16:47
 * @desc 战嚎
 **/
@Component
public class BattleCryEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        int count = MapUtils.getIntValue(ctx, COUNT);
        ChessBoard chessBoard = room.getChessBoard();
        Set<Integer> indexs = new HashSet<>();
        Grid[] grids = chessBoard.getGrids();
        do {
            int index = RandomUtils.nextInt(grids.length);
            indexs.add(index);
        } while (indexs.size() != count);
        for (Integer index : indexs) {
            grids[index].fill(new Chess(ChessState.LIGHTNING3, ChessType.CT_LIGHTNING));
        }
        skillReport.setIndexs(indexs);//3级闪电索引
        //自动消除
        autoElimi(room, owner, skillReport, chessBoard);
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_BATTLE_CRY;
    }
}
